package com.zs.grain

import android.content.BroadcastReceiver
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.media.AudioManager
import android.widget.Toast
import com.example.clockkotlin.databaseClockAlarm.AlarmSignal
import com.jeremyliao.liveeventbus.LiveEventBus
import com.xnkj1688.semplibrary.entity.DaoSession
import com.zs.grain.service.DataService
import com.zs.grain.service.DeviceService

import com.zs.grain.service.SoundService
import com.zs.grain.tool.clockManage.LocalDataBase
import com.zslibrary.BusMaster.BusMaster
import com.zslibrary.BusMaster.HandleError
import java.util.*
import java.util.logging.Handler
import javax.inject.Inject
import kotlin.collections.ArrayList
import kotlin.concurrent.timerTask

class AlarmReceiver: BroadcastReceiver() {

    // = null
    //private var mSoundService: SoundService?=null
    override fun onReceive(context: Context?, intent: Intent?) {
//        var mDeviceService=DeviceService()
//        if(!mDeviceService.errorsString.isEmpty())
//        Run.after(30000,{
//            if(false){
//
//            }else{
//                Toast.makeText(context,"30秒后系统重启",Toast.LENGTH_SHORT).show()
//            }
//        })

//        Handler().postDelayed({
//            //doSomethingHere()
//        }, 1000)
        var rebootNow = true
        val clocksArray:ArrayList<AlarmSignal> = LocalDataBase.getClocksArray()
        for(ca in clocksArray){
            if(ca.setting.contains("makingProduct")){
                if(!ca.enable) {
                    LocalDataBase.deleteAlarm("makingProduct")
                    LocalDataBase.deleteAlarm("waitingReboot")
                    setRebootEnable(context,!ca.enable)
                }else {
                    rebootNow = false
                    LocalDataBase.addAlarm("01:01","waitingReboot")
                }
            }//else setRebootEnable(context,true)

//            if(ca.setting.contains("reboot")){//有异常
//                if(!ca.enable) rebootNow = false
//            }
        }
        if(HandleError.getNumberOfError()>0) rebootNow=false
        setRebootEnable(context,rebootNow)
        //LiveEventBus.get("startReboot").post(true)
    }

    fun setRebootEnable(context: Context?,rebootEnable:Boolean){
        if(rebootEnable) {
            LiveEventBus.get("AutoRestart").post(true)
            Toast.makeText(context,"机器即将进入维护状态,10秒后自动重启...",Toast.LENGTH_LONG).show();

            val timer = Timer()
            timer.schedule(timerTask {
                BusMaster.getInstance(context).resetBoard()
                val componetName = ComponentName(
                    "com.example.reboottest",
                    "com.example.reboottest.MainActivity"
                );
                val intent = Intent();
                intent.setComponent(componetName);
                context?.startActivity(intent);
            }, 10000)
        }
    }
}
//class Run{
//    companion object{
//        fun after(delay:Long,process:()->Unit){
//            Handler().postDelayed({
//                process()
//            }, delay)
//        }
//    }
//}